{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Data Downloaded 04/10/2020\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "import itertools as IT\n",
    "import requests\n",
    "import json"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Source Files\n",
    "fname = 'https://data.bls.gov/cew/data/files/2018/csv/2018_annual_singlefile.zip'\n",
    "\n",
    "# Chunk Size\n",
    "chunksize = 10 ** 3\n",
    "\n",
    "# Load Data for county total emp\n",
    "chunks = pd.read_csv(fname, chunksize = chunksize)\n",
    "df_county_tot = pd.concat([chunk[(chunk['agglvl_code'] == 70)].copy() for chunk in chunks])\n",
    "\n",
    "# Load Data for state industry data\n",
    "chunks = pd.read_csv(fname, chunksize = chunksize)\n",
    "df_state_ind = pd.concat([chunk[(chunk['agglvl_code'] == 55)].copy() for chunk in chunks])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "df_state_ind_all = df_state_ind.groupby(['area_fips','industry_code'])[['annual_avg_emplvl']].sum().reset_index()\n",
    "df_state_ind_all.rename(columns={'industry_code' : 'naics_code'},inplace=True)\n",
    "\n",
    "df_state_ind_all['STATE'] = df_state_ind_all.area_fips/1000\n",
    "\n",
    "df_state_ind_all.to_csv('../../data/bls/state_emp_ind.csv', index = False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "df_county_tot = df_county_tot[['area_fips','annual_avg_emplvl']]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "df_county_tot.rename(columns={'area_fips':'countyFIPS'}, inplace=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "df_county_tot.to_csv('../../data/bls/county_qcew_emp.csv')"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.3-final"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}